//
//  CityPickerView.swift
//  Copy_Aikaopu
//
//  Created by taojianz on 2017/6/30.
//  Copyright © 2017年 taojianz. All rights reserved.
//

import UIKit

class CityPickerView: UIPickerView , UIPickerViewDelegate , UIPickerViewDataSource {

    // 地区集合的Pilist
    var regionDict = NSDictionary(contentsOfFile: Bundle.main.path(forResource: "address", ofType: "plist")!)
    
    var selectedCity : String? = "北京 北京市 东城区"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.dataSource = self
        self.delegate = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        guard let regionDict = regionDict,
            let provincesArray = regionDict["address"] as? NSArray
        else{
                print("无法获取地区集合")
                return 0
        }
        if component == 0{
            
            let componentNumber = provincesArray.count
            
            return componentNumber
        }
        if component == 1{
            let provinceSelectedRow = selectedRow(inComponent: 0)
            
            return (((provincesArray[provinceSelectedRow] as! NSDictionary)["sub"]) as! NSArray).count
            
        }
        if component == 2{
            let provinceSelectedRow = selectedRow(inComponent: 0)
            
            let citiesSelectedRow = selectedRow(inComponent: 1)
            
            return (((((provincesArray[provinceSelectedRow] as! NSDictionary)["sub"]) as! NSArray)[citiesSelectedRow] as! NSDictionary)["sub"] as! NSArray).count
        }
        
        
        return 0
    }
    // 为每个 row 设置名字
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        guard let regionDict = regionDict,
            let provincesArray = regionDict["address"] as? NSArray
            else{
                print("无法获取地区集合")
                return nil
        }
        if component == 0{
            
            let provinceName = (provincesArray[row] as! NSDictionary)["name"] as! String
            
            return provinceName
        }
        if component == 1{
            let provinceSelectedRow = selectedRow(inComponent: 0)
            
            let cityName = (((provincesArray[provinceSelectedRow] as! NSDictionary)["sub"]as! NSArray)[row] as! NSDictionary)["name"] as! String
            
            return cityName
        }
        if component == 2{
            let provinceSelectedRow = selectedRow(inComponent: 0)
            
            let citiesSelectedRow = selectedRow(inComponent: 1)
            
            let areaName = ((((provincesArray[provinceSelectedRow] as! NSDictionary)["sub"]as! NSArray)[citiesSelectedRow] as! NSDictionary)["sub"] as! NSArray)[row] as! String
            
            return areaName
        }
        
        
        return nil
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if component == 0{
            reloadComponent(1)
            reloadComponent(2)
            selectRow(0, inComponent: 1, animated: true)
            selectRow(0, inComponent: 2, animated: true)
        }
        
        if component == 1{
            reloadComponent(2)
            selectRow(0, inComponent: 2, animated: true)
        }
        
    }
    
}
